<template>
    <div class="back_contain_n">
        <!-- 头部搜索 -->
        <div class="Search_Top_Part">
            <div class="search_list" >
                <div>
                    <div class="input_flex">
                        <el-select clearable v-model="query.jobCategory" placeholder="岗位类别">
							<el-option label="管理" value="A"></el-option>
							<el-option label="操作" value="Y"></el-option>
                        </el-select>
                    </div>
                    <div class="input_flex">
                        <el-select clearable v-model="query.deptId" placeholder="部门">
                            <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id"></el-option>
                        </el-select>
                    </div>
                    <div class="input_flex">
                        <el-input clearable v-model="query.jobNumber" placeholder="工号"></el-input>
                    </div>
                    <div class="input_flex">
                        <el-input clearable v-model="query.name" placeholder="姓名"></el-input>
                    </div>
                    <div class="input_flex">
                        <el-select clearable v-model="query.gender" placeholder="性别">
                            <el-option label="男" value="M"></el-option>
                            <el-option label="女" value="F"></el-option>
                        </el-select>
                    </div>
                    <div class="input_flex">
                        <el-select clearable v-model="query.status" placeholder="状态">
                            <el-option label="试用在职" value="0"></el-option>
                            <el-option label="合格在职" value="1"></el-option>
                            <el-option label="离职" value="2"></el-option>
                        </el-select>
                    </div>
                    <div class="input_flex daterange">
                        <el-date-picker v-model="time"
                            type="daterange"
                            range-separator="-"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                        </el-date-picker>
                    </div>
                </div>
            </div>
            <div class="search_bt">
                <span class="zll-search" @click="getList">搜索</span>
                <span class="zll-search-reset" @click="searchReset()">重置</span>
            </div>
        </div>
        <!-- table -->
        <sys-table  
            :tableData="tableData" 
            :tableLoading="tableLoading" 
            :tableHeader="tableHeader"
            :scopeWidth="120"
        >
            <template slot-scope="scope" slot="operate">
				<el-button @click="edit(scope.row,'see')" type="text" size="small">查看</el-button>
				<el-button v-if="scope.row.onboardingStatus != '2'" @click="edit(scope.row,'edit')" type="text" size="small">编辑</el-button>
				<el-button @click="deleteRow(scope.row)" type="text" size="small">删除</el-button>
            </template>
        </sys-table>
        <div class="zll-dialog">
            <popout :title="'员工 · ' + (type == 'edit'?'管理':(type =='see'?'信息':'注册'))" :visible.sync="addDialog" v-if="addDialog" class="hr">
                <add ref="add" slot="content" :editData="editData" :type="type" :deptList="deptList" @refreshData="refreshData"></add>
                <template slot="bottom">
                    <p class="zll-botton Info" @click="addDialog = false">关 闭</p>
                    <p class="zll-botton" v-if="type !== 'see'" @click="()=>{this.$refs.add.submitForm('addForm')}">提交</p>
                </template>
            </popout>
        </div> 
    </div>
</template>

<script>
import Add from './add'
import {formatDate} from "../../../../../plugins/formatDate";
export default {
    data(){
        return {
            tableLoading:true, //table刷新
            type: '',
            editData: {},

            query: {
				jobCategory: '',
				deptId: '',
				jobNumber: '',
				name: '',
				gender: '',
				status: ''
            },
            tableData: [
                {}
            ],
            tableHeader:[],
            addDialog: false,

			time:[],

			deptList: []
        }
    },
    methods: {

        getList(){//获取表格数据
			let self = this;

			if (this.time && this.time.length == 2){
				let startDate = new Date(self.time[0]);
				let endDate = new Date(self.time[1]);
				if (endDate.getTime() - startDate.getTime() > 365*24*3600*1000){
					self.$message.warning('时间间隔最多为1年');
					return;
				}
				self.query.startTime = formatDate(startDate,'yyyy-MM-dd');
				self.query.endTime = formatDate(endDate,'yyyy-MM-dd') + ' 23:59:59';
			}

			self.tableData = [];
			self.tableLoading = true;

			self.$http({
				url: "/jinl/personnelManage/getAllPersonnelList",
				method: "post",
				data: JSON.stringify(self.query),
				dataType: 'json',
				contentType: 'application/json',
			}).then(resp=>{
				if (resp.success) {

					self.tableHeader =  [
						{"columnValue": "jobCategoryCn", "columnName": "岗位类别" },
						{"columnValue": "deptName", "columnName": "部门" },
						{"columnValue": "jobNumber", "columnName": "工号" },
						{"columnValue": "employeeName", "columnName": "姓名" },
						{"columnValue": "genderCn", "columnName": "性别"},
						{"columnValue": "joinedDate", "columnName": "入职日期" },
						{"columnValue": "jobyear", "columnName": "工龄" },
						{"columnValue": "onboardingStatusCn", "columnName": "入职状态" },
					];

					for (let data of resp.result){
						data.createrName = data.createrName+"  "+data.createTimeFt;

						if (data.ensuerName) {
							data.ensuerName = data.ensuerName+"  "+data.ensureDate;
						}

						if (data.operatorName) {
							data.operatorName = data.operatorName+"  "+data.operatorDate;
						}

						//头像处理
						data.fileCheck = parseInt(data.fileCheck);
						data.fileBlob = data.fileUrl;
						data.fileName = data.displayName;
					}

					self.tableData = resp.result;
					self.tableLoading = false;

				}

			})
        },

		getDeptList(){//获取部门下拉列表
        	let self = this;
        	self.$http({
				url: "/jinl/dept/queryDeptChooseList",
				method: "post",
			}).then(resp=>{
				if (resp.success) {
					if (resp.result.length > 0){
						self.deptList = resp.result;
					}
				}
			});
		},

		deleteRow(rowData) {
			let self = this;
			self.$confirm('是否确认删除？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				self.$http({
					url: '/jinl/personnelManage/deletePersonnelManagement',
					method: "post",
					data: JSON.stringify({ids: [rowData.id]}),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp => {
					if (resp.success) {
						self.getList();
						self.$message.success('删除成功！');
						return
					}
					self.$message.error(resp.message);
				});
			});
		},

        edit(val,type) {
			let self = this;
			self.addDialog = true;

			self.$nextTick(()=> {
				self.type = type;
				self.editData = Object.assign({},val);
			});
        },

		refreshData() {
			this.getList();
			this.addDialog = false;

		},

        searchReset() { //重置搜索
			this.query = {
				jobCategory: '',deptId: '',jobNumber: '',name: '',gender: '',status: ''
			};
			this.time = [];
            this.getList();
        },


    },
    mounted(){
        let self = this;
        self.getDeptList();
        self.getList();
        self.$eventBus.$on("updateWorkList",function () {
            self.addDialog = false;
            self.getList();
        })
    },
    components: {
        Add
    },
}
</script>

<style lang="scss" scoped>
@import "@/style/SearchTop.scss";
</style>